package com.zrrd.javaWeb.servlet;

import com.alibaba.fastjson.JSON;
import com.zrrd.javaWeb.bean.User;
import com.zrrd.javaWeb.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title QueryAllUserServlet
 * @description
 * @create 2024/1/19 16:26
 */
@WebServlet("/queryAllUser")
public class QueryAllUserServlet extends HttpServlet {
    private UserService userService=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = userService.queryAll();
        //利用resp响应，把查询到的users集合，返回给前端。
        //1、把Java对象转换成JSON
        String users = JSON.toJSONString(list);
        //2、把JSON数据响应给前端页面
        resp.getWriter().write(users);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
